//
//  ViewController.m
//  book
//
//  Created by cocoazxj@163.com on 15/4/7.
//  Copyright (c) 2015年 handcool. All rights reserved.
//

#import "ViewController.h"
#import "ContactViewController.h"
#import "MBProgressHUD.h"
#import "ContactModel.h"

typedef float (^MyBlock)(int a, int b);
@interface ViewController ()<MBProgressHUDDelegate>
@property (nonatomic, strong) IBOutlet UITextField *tf_userName;
@property (nonatomic, strong) IBOutlet UITextField *tf_password;
@property (nonatomic, strong) IBOutlet UIButton *btn_login;
@property (nonatomic, strong) IBOutlet UISwitch *s_remeber;
@property (nonatomic, strong) IBOutlet UISwitch *s_auto;
@property (nonatomic, strong) NSUserDefaults *userDefaults;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.tf_userName];
    [center addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.tf_password];
    
    self.userDefaults = [NSUserDefaults standardUserDefaults];
    self.s_remeber.on = [self.userDefaults boolForKey:@"rem"];
    self.s_auto.on = [self.userDefaults boolForKey:@"auto"];
    
    if([self.userDefaults boolForKey:@"rem"])
    {
        self.tf_userName.text = [self.userDefaults objectForKey:@"name"];
        self.tf_password.text = [self.userDefaults objectForKey:@"password"];
    }
    
    if(self.tf_password != nil && self.tf_userName != nil)
    {
        self.btn_login.enabled = NO;
    }
    
    if([self.userDefaults boolForKey:@"auto"])
    {
        [self loginButtonClicked:self.btn_login];
    }
    
//    float (^myBlock)(int a, int b);
    MyBlock myBlock = ^(int a, int b){
        float c;
        c = a + b;
        return c;
    };
    
    [self testBlock:myBlock];
    [self query:@"zxj" testNoNameBlock:^(int a, int b){
//        NSLog(@"----------%d",a + b);
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tf_userName becomeFirstResponder];
    if([self.userDefaults boolForKey:@"rem"])
    {
        self.btn_login.enabled = YES;
    }
    else
    {
        self.btn_login.enabled = NO;
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)textChange
{
    self.btn_login.enabled = (self.tf_password.text.length > 0 && self.tf_userName.text.length > 0);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)remeberButtonClicked:(UISwitch *)sender
{
    if(!self.s_remeber.on)
    {
        NSLog(@"-------s_remeber--------YES");
        [self.s_auto setOn:NO animated:YES];
        [self.userDefaults setBool:NO forKey:@"auto"];
        [self.userDefaults setObject:@"" forKey:@"password"];
        [self.userDefaults setObject:@"" forKey:@"name"];
    }
    [self.userDefaults setBool:self.s_remeber.on forKey:@"rem"];
}

- (IBAction)autoButtonClicked:(UISwitch *)sender
{
    if(self.s_auto.on)
    {
        NSLog(@"-------s_auto--------YES");
        [self.s_remeber setOn:YES animated:YES];
        [self.userDefaults setBool:YES forKey:@"rem"];
    }
    [self.userDefaults setBool:self.s_auto.on forKey:@"auto"];
}

- (IBAction)loginButtonClicked:(id)sender
{
    
    if(![self.tf_userName.text isEqualToString:@"zz"])
    {
        MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.view];
        [self.view addSubview:HUD];
        HUD.mode = MBProgressHUDModeText;
        HUD.labelText = @"Hello HUD!";
        HUD.labelColor = [UIColor whiteColor];
        [HUD show:YES];
        [HUD hide:YES afterDelay:1.0];
        return ;
    }
    
    if (![self.tf_password.text isEqualToString:@"zz"])
    {
        return ;
    }
    [self.userDefaults setObject:self.tf_password.text forKey:@"password"];
    [self.userDefaults setObject:self.tf_userName.text forKey:@"name"];
    [self.userDefaults synchronize];
    if(![self.userDefaults boolForKey:@"rem"])
    {
        self.tf_userName.text = @"";
        self.tf_password.text = @"";
    }
    
    [self performSegueWithIdentifier:@"loginToContact" sender:nil];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UIViewController *contactVC = (ContactViewController *)segue.destinationViewController;
    contactVC.title = @"联系人列表";
}

// 有名字的Block
- (void)testBlock:(MyBlock)block
{
    //    NSLog(@"----%f",block(1,2));
}

// 无名字的Block
- (void)query:(NSString *)name testNoNameBlock:(void(^)(int a, int b))block
{
    static NSString  *d = @"";
    [self query:name netBlock:^(NSString *c){
        //        NSLog(@"c----------%@",c);
        d = c;
    }];
    block([d intValue],5);
}

// 需要加上void,不然报错：Called object type 'id' is not a function or function pointer。
- (void)query:(NSString *)name netBlock:(void(^)(NSString *))block
{
    // 获取数据
    if([name isEqualToString:@"zxj"])
    {
        block(@"6");
    }
    
}
@end
